<?php

namespace frontend\controllers;


use bc\controller\SampleController;
use bc\controller\WxOAuthController;
use bc\model\ProductJpgModel;
use bc\model\ProductModel;
use bc\model\ProductsampleModel;
use bc\model\wc\AgentModel;
use bc\model\product\QrcodeModel;
use yii\web\Session;
use bc\service\SpokesmanService;
use bc\model\product\DistributionModel;
use bc\util\Math;
use bc\model\SpokesmanModel;

class ProductController extends SampleController
{
	public $aview;

	public function init()
	{
		parent::init(); // TODO: Change the autogenerated stub
		// 生成js sdk签名
		$this->getRequest()->hostInfo;
		$jsConfig = $this->configJsApi();
		$this->aview ['js_config'] = $jsConfig;
	}

	public function actionIndex()
	{
		
	}
	
	
	/**
	 * 商品详情页
	 */
	public function actionDetail()
	{
		$id = $this->get('id',2);
		if($id == 1)
			$id = 2;
		$this->getHtmlFromPid($id);
		$productModel = new ProductModel();
		$product = $productModel->getRow($id);
		$agent_id = $this->get('agent_id');
		
		if(!$product || $product['is_sale'] == 2 || $product['status'] == 2)
			$this->redirect('/');
		
		$view['product'] = $product;
		
		
		$session = new Session();
		$user = $session->get('shop_user');
		
		if($product['product_id'] == 1)
		{
			if($agent_id)
			{
				$agentModel = new AgentModel();
				$agent = $agentModel->getRowById($agent_id);
				if($agent)
				{
					//设置COOKIE 有效期一天
					$rspCookies = $this->response()->cookies;
					$rspCookies->add(new \yii\web\Cookie([
							'name' => 'recommend_wc',
							'value' => $agent['user_id'],
							'expire'=>time()+3600*24,
					]));
				}
				
			}
			
			
			//无创销售
			$template = '';
		}
		else if($product['product_id'] == 2)
		{
			$view ['jsapi']['share_url']=$this->getRequest()->hostInfo.'/product/detail?id='.$product['product_id'];
			$view ['jsapi']['share_title']='滚蛋吧，肥胖君';
			$view ['jsapi']['share_content']='找到DNA中的减肥机密';
			$view ['jsapi']['js_config']=$this->aview ['js_config'];
			$view ['product_html']=$this->html;
			//肥胖基因
			$template = 'fpgene';
			
		}
		else if($product['product_id'] == 3)
		{
			$view ['jsapi']['share_url']=$this->getRequest()->hostInfo.'/product/detail?id='.$product['product_id'];
			$view ['jsapi']['share_title']='维D该补多少？竟是它说了算！';
			$view ['jsapi']['share_content']='了解身体，精准补充。妈妈再也不用担心我补不够or过量啦！';
			$view ['jsapi']['js_config']=$this->aview ['js_config'];
			$view ['product_html']=$this->html;
			//肥胖基因
			$template = 'fpgene';

		}
		else 
		{
			$view ['jsapi']['share_url']=$this->getRequest()->hostInfo.'/product/detail?id='.$product['product_id'];
			$view ['jsapi']['share_title']=$product['short_description'];
			$view ['jsapi']['share_content']=$product['product_name'];
			$view ['jsapi']['js_config']=$this->aview ['js_config'];
			$view ['product_html']=$this->html;
			$productjpgM = new ProductJpgModel();
			$productjpgD=$productjpgM->getList(['product_id'=>$product['product_id']]);
			$view ['product_jpg']=$productjpgD;
			$template = 'basic';
		}
		
		
		$product_price = $product['product_price'];
		
		//分销商品
		$distributionModel = new DistributionModel();
		$distribution = $distributionModel->getRow(['product_id' => $id]);
		if($distribution)
		{
			$view['distribution'] = $distribution;
			$product_price = $distribution['distribution_price'];
		}
		
		
		//判断是否推广优惠的会员
		$view['first_buy'] = false;
		$code = $this->get('c');
		if($code)
		{
			$qrcodeModel = new QrcodeModel();
			$qrcodeInfo = $qrcodeModel->getRow(['product_id' => $id, 'code' => $code, 'is_delete' => 0]);
			if($qrcodeInfo)
			{
				
				if($qrcodeInfo != $user['user_id'])
				{
					//判断用户改商品是否首次购买
					$spokesmanService = new SpokesmanService();
					if($spokesmanService->firstBuy($id, $user))
					{
						//获取绑定推广商品信息 二维数组
						$recommendProduct = $session->get('recommend_product');
						$recommendProduct[$id]['parent_user_id'] = $qrcodeInfo['user_id'];
						$session->set('recommend_product', $recommendProduct);
						
						$product_price = $distribution['distribution_price'] - Math::ConverToCeil($distribution['distribution_price'] * $distribution['first_discount'], 1);
						
						$discount_price = Math::ConverToCeil($distribution['distribution_price'] * $distribution['first_discount'], 1);
						
						$view['discount_price'] = $discount_price;
						$view['first_buy'] = true;
		
					}
						
				}
			}
				
		}
		
		//是否代言人
		$qrcodeModel = new QrcodeModel();
		$qrcode = $qrcodeModel->getRow(['product_id' => $product['product_id'], 'user_id' => $user['user_id'], 'is_delete' => 0]);
		if($qrcode)
		{
			$view ['jsapi']['share_url']=$this->getRequest()->hostInfo.'/product/detail?id='.$product['product_id'] . '&c=' . $qrcode['code'];
		}
		
		$view['product_price'] = $product_price;
		return $this->render($template, $view);
	}
}

